<template>

  <div class="app-container">

    <h2 style="text-align: center;">发布新课程</h2>

    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="提交审核"/>
    </el-steps>

    <el-button type="text" @click="dialogChapterFormVisible=true">添加章节</el-button>
    <!-- 章节 -->
    <ul class="chanpterList">
      <li
        v-for="chapter in chapterList"
        :key="chapter.id">
        <p>
          {{ chapter.title }}

          <span class="acts">
                <el-button type="text" @click="openVideoEdit();video.chapterId = chapter.id">添加小节</el-button>
                <el-button style="" type="text" @click="openEditChapter(chapter.id)">编辑</el-button>
                <el-button type="text" @click="deleteCourse(chapter.id)">删除</el-button>
            </span>
        </p>

        <!-- 视频 -->
        <ul class="chanpterList videoList">
          <li
            v-for="video in chapter.children"
            :key="video.id">
            <p>
              {{ video.title }}
              <span class="acts">
                        <el-button type="text" @click="openVideoEdit4Update(video.id)">编辑</el-button>
                        <el-button type="text" @click="removeVideo(video.id)">删除</el-button>
                    </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>


    <!-- 添加和修改章节表单 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"/>
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加和修改课时表单 -->
    <el-dialog :visible.sync="dialogVideoFormVisible" title="添加课时">
      <el-form :model="video" label-width="120px">
        <el-form-item label="课时标题">
          <el-input v-model="video.title"/>
        </el-form-item>
        <el-form-item label="课时排序">
          <el-input-number v-model="video.sort" :min="0" controls-position="right"/>
        </el-form-item>
        <el-form-item label="是否免费">
          <el-radio-group v-model="video.isFree">
            <el-radio :label="0">免费</el-radio>
            <el-radio :label="1">默认</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上传视频">
          <el-upload
            :on-success="handleVodUploadSuccess"
            :on-remove="handleVodRemove"
            :before-remove="beforeVodRemove"
            :on-exceed="handleUploadExceed"
            :file-list="fileList"
            :action="BASE_API+'/ali/oss/fileoss/uploadVideo'"
            :limit="1"
            class="upload-demo">
            <el-button size="small" type="primary">上传视频</el-button>
            <el-tooltip placement="right-end">
              <div slot="content">最大支持1G，<br>
                支持3GP、ASF、AVI、DAT、DV、FLV、F4V、<br>
                GIF、M2T、M4V、MJ2、MJPEG、MKV、MOV、MP4、<br>
                MPE、MPG、MPEG、MTS、OGG、QT、RM、RMVB、<br>
                SWF、TS、VOB、WMV、WEBM 等视频格式上传
              </div>
              <i class="el-icon-question"/>
            </el-tooltip>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVideoFormVisible = false">取 消</el-button>
        <el-button :disabled="saveVideoBtnDisabled" type="primary" @click="saveOrUpdateVideo">确 定</el-button>
      </div>
    </el-dialog>
    <el-form label-width="120px">

      <el-form-item>
        <el-button @click="previous">上一步</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>


</template>

<script>
import chapter from "@/api/edu/chapter.js";
import course from "../../../api/edu/course";
import video from "@/api/edu/video.js";
import vi from "element-ui/src/locale/lang/vi";

export default {
  data() {
    return {
      saveBtnDisabled: false,
      saveVideoBtnDisabled: false,
      chapterList: [], //列表章节
      courseId: '', //课程id，通过路由转发传过来的
      chapter: {
        title: '',
        sort: '',
        courseId: '',
      },
      video: {
        title: '',
        isFree: 0,
        videoSourceId: '',
        videoOriginalName: '',
      },
      dialogChapterFormVisible: false, //章节信息框
      dialogVideoFormVisible: false, //小节信息框
      fileList: [], //上传
      BASE_API: process.env.BASE_API,


    }
  },
  created() {
    if (this.$route.params && this.$route.params.id) {
      //判断传进来是否有参数和参数是否为id
      //如果有id这个参数传递给courseId
      this.courseId = this.$route.params.id;
      //调用查询章节函数
      this.getAllSubjectAndVideo();
    }
  },


  methods: {
    //=========================上传视频==========================================
    /**
     * 上传成功调用的方法
     * @param response 得到的结果
     * @param file 上传的文件
     * @param fileList
     */
    handleVodUploadSuccess(response, file, fileList) {

      //获取视频id
      this.video.videoSourceId = response.data.url;

      //获取文件名
      this.video.videoOriginalName = file.name;
    },

    /**
     * 视频已经存在的时候调用的方法
     */
    handleUploadExceed() {
      this.$message.warning("想要上传视频，请先删除当前视频");
    },
    /**
     * 点击 x 弹出删除确认框
     * @param file
     * @param fileList
     * @returns {*}
     */
    beforeVodRemove(file, fileList) {
      return this.$confirm(`确认移除${file.name}?`);

    },
    /**
     * 点击确认删除后执行的方法
     * 是真正删除视频的方法
     */
    handleVodRemove() {
      //调用接口中的方法删除视频
      video.removeVideoId(this.video.videoSourceId).then(result => {
        this.$message({
          type: 'success',
          message: '删除视频成功'
        });
        //将文件清空
        this.fileList = [];
        //将文件上传id清空
        this.video.videoSourceId = ''
        //清空文件名
        this.video.videoOriginalName = '';
      })
    },

    //=========================小节操作==========================================
    openVideoEdit() {
      this.video.chapterId = this.chapter.id;
      this.video.courseId = this.courseId;
      this.dialogVideoFormVisible = true;

    },
    /**
     * 更新时候弹出的框
     * @param videoId
     */
    openVideoEdit4Update(videoId) {
      this.dialogVideoFormVisible = true;
      video.getVideo(videoId).then(result => {
        //在这里就将所有参数传递给了前端
        this.video = result.data.video;
      })
    },
    /**
     * 添加小节
     */
    addVideo() {
      this.video.courseId = this.courseId;
      video.addVideo(this.video).then(result => {
        //提示添加成功
        this.$message({
          type: 'success',
          message: '添加小节成功'
        })
        //关闭窗口
        //清空原有的数据
        this.video = {}
        this.video.isFree = 1;
        this.dialogVideoFormVisible = false;
        //更新数据
        this.getAllSubjectAndVideo();
      })
    },
    removeVideo(videoId) {
      video.deleteVideo(videoId).then(
        result => {
          this.$confirm(`此操作将永久删除该小节, 是否继续?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            //点击了确认执行该方法
            this.$message({
              type: 'success',
              message: '删除小节成功!'
            });
            //刷新界面
            this.getAllSubjectAndVideo()

          })
        }
      )
    },
    /**
     * 更新小节
     * @param video
     */
    updateVideo() {
      //修改小节
      video.updateVideo(this.video)
        .then(result => {
          //关闭弹窗
          this.dialogVideoFormVisible = false;
          //提示更新成功
          this.$message({
            type: 'success',
            message: '更新小节成功'
          });
          this.getAllSubjectAndVideo();
          //刷新界面
          this.video = {}
          //清空video中的数据
          this.video.isFree = 1;

        })
    },
    /**
     * 更新或者添加小节
     */
    saveOrUpdateVideo() {
      if (!this.video.id) {
        //如果没有video的id就添加小节
        this.addVideo();
      } else {
        //如果有id就执行修改
        this.updateVideo();
      }

    },


    //=========================章节操作==========================================
    /**
     * 根据courseId获取所有章节
     */
    getAllSubjectAndVideo() {
      chapter.getAllSubjectVideo(this.courseId).then(result => {
        this.chapterList = result.data.list;
        console.log(this.chapterList)
      })
    },
    /**
     * 去到下一个界面的处理
     */
    next() {
      this.$router.push({path: '/edu/course/publish/' + this.courseId})
    },
    /**
     * 调转到上一个界面
     */
    previous() {
      this.$router.push({path: '/edu/course/info/' + this.courseId})
    },
    /**
     * 保存章节
     */
    addChapter() {
      this.chapter.courseId = this.courseId;
      //添加章节
      chapter.addChapter(this.chapter).then(result => {
        //关闭窗口
        this.dialogChapterFormVisible = false;
        //提示添加章节成功
        this.$message({
          type: 'success',
          message: '添加章节成功'
        })
        //更新章节信息
        this.getAllSubjectAndVideo();
        this.chapter = {};
      })
    },
    /**
     * 更新章节信息
     */
    updateChapter() {
      //更新章节信息
      chapter.updateChapter(this.chapter).then(result => {
        //关闭弹窗
        this.$message({
          type: 'success',
          message: '更新成功'
        });
        //关闭窗口
        this.dialogChapterFormVisible = false;
        //清空里面的信息
        this.chapter = {};
        //刷新界面
        this.getAllSubjectAndVideo();
      })

    },
    /**
     * 保存或者更新章节
     */
    saveOrUpdate() {
      if (!this.chapter.id) {
        //如果没有id就执行添加
        this.addChapter()
      } else {
        console.log(this.chapter)
        //如果有id就更新
        this.updateChapter()

      }
    },
    /**
     * 根据章节id删除章节
     * @param chapterId
     */
    deleteCourse(chapterId) {
      this.$confirm(`此操作将永久删除该章节, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //点击了确认执行该方法
        chapter.deleteChapter(chapterId)
          .then(result => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            //跳转到第一页
            this.getAllSubjectAndVideo()
          })
      })
    },
    /**
     * 更新回显章节信息
     * @param chapterId
     */
    openEditChapter(chapterId) {
      chapter.getChapter(chapterId).then(result => {
        //信息填写框可见
        this.dialogChapterFormVisible = true;
        //赋值
        this.chapter = result.data.chapter;
      })

    }
  }

}
</script>

<style scoped>
/*.chanpterList {*/
/*  position: relative;*/
/*  list-style: none;*/
/*  margin: 0;*/
/*  padding: 0;*/
/*}*/

/*.chanpterList li {*/
/*  position: relative;*/
/*}*/

/*.chanpterList p {*/
/*  !*float: left;*!*/
/*  font-size: 20px;*/
/*  margin: 10px 0;*/
/*  padding: 10px;*/
/*  height: 70px;*/
/*  line-height: 50px;*/
/*  width: 100%;*/
/*  border: 1px solid #DDD;*/
/*}*/

/*.chanpterList .acts {*/
/*  float: right;*/
/*  font-size: 14px;*/
/*}*/

/*.videoList {*/
/*  padding-left: 50px;*/
/*}*/

/*.videoList p {*/
/*  float: left;*/
/*  font-size: 14px;*/
/*  margin: 10px 0;*/
/*  padding: 10px;*/
/*  height: 50px;*/
/*  line-height: 30px;*/
/*  width: 100%;*/
/*  border: 1px dotted #DDD;*/
/*}*/

</style>
